(function() {
	'use strict';
	define('card', ['jquery', 'form'], function($) {
		if(typeof jQuery === 'undefined') {
			throw new Error('组件[card]加载失败，缺少依赖[jquery]');
		}
		
		/** e: 元素id
		 * options: 属性对象
		 * return: card对象
		 * */
		function newInstance(e, options) {
			var $e = $(e), opts = $.extend({element: e}, this, options);
			var cardsId = options.cardsId;
			
			var str = '<div id="'+ cardsId +' class="gridly"> </div>';
			if(opts.data.length != 0){//有数据的情况下初始化列表
				
				initCards(opts);
			}
			
			return opts;
		}
		
		//使用数据初始化卡片
		function initCards(cardObj) {
			var element = $('#' + cardObj.cardsId),
				data = cardObj.data,
				fields = cardObj.fields;
			var i, j, cardData, card, cardDiv;
			for(i = 0, j= data.length; i<j; i++){
				cardData = data[i];
				card = {
						id: cardData[fields.id],
						date: cardData
				};
				cardObj.cards.push(card);
				cardDiv = _cardDiv(data, fields);
				
				_bindRemoveList($(cardDiv).find('#notice_delete'), cardObj);
				_bindEditList($(cardDiv).find('#update_info'), cardObj);
				
				element.append(cardDiv);
			}
			var addCardDiv = _addCardDiv();	//添加-添加列表div
			
			return cardObj;
		}
		
		//生成每一个卡片
		function _cardDiv(data, fields) {
			var hitsNumber = data[fields.hitsNum],
				title = data[fields.title],
				id = data[fields.id],
				hitsDiv = '<strong><span class="click-card">点击量'+ hitsNumber +'</span></strong>',
			    deleteDiv = '<a id="notice_delete" class="delete" href="javascript:void(0)">&times;</a>',
			    titleDiv = '<p>'+ title +'</p>',
			    editDiv = '<div class="meta">'+
			        '<i class="fa fa-fw fa-user fa-eye no-font" title="查看详情" id="detail_info"></i>'+
			        '<i class="fa fa-fw fa-user fa-pencil-square-o no-font" id="update_info" title="编辑"></i>'+
			    '</div>',
			    cardDiv = $('<div class="brick large" fieldId = "'+id+'"></div>')
			    		.append(hitsDiv).append(deleteDiv).append(titleDiv).append(editDiv);
			return cardDiv;
		}
		
		//添加div
		function _addCardDiv() {
			return $('<div class="add-card"><i id="notice_add" class="fa fa-fw fa-user fa-plus-square-o la-font" title="添加"></i></div>');
		}
		
		//绑定事件
		function _bindAddEvent(e,cardObj) {
			e.find('#notice_add').bind('click', function(){
				_trigger('', 'onAdd', cardObj);
			})
		}
		
		//添加
		function _addCardBefore(e, cardObj) {
			var data=cardObj.data[0], 	//需要一个data
			fields = cardObj.fields,
			cardDiv = _cardDiv(data, fields);	//调用生成卡片
			
			cardObj.data.push(data);	//添加到data
			cardObj.cards.push(data);	//添加到card中
			e.before(cardDiv);
			_bindRemoveList($(cardDiv).find('#notice_delete'), cardObj);
			_bindEditList($(cardDiv).find('#update_info'), cardObj);
			_bindDetailList($(cardDiv).find('#detail_info'), cardObj);
		}
		
		//绑定删除列表
		function _bindRemoveList(e, cardObj) {
			var el = e.parent();	//找到
			e.bind('click', function(e) {
				//分为三种情况  1--undefined：,2--true：,3--false:
				_trigger({item:_getCardData(el, cardObj), el: el}, 'onRemove', cardObj);
			});
		}
		
		//绑定编辑列表
		function _bindEditList(e, cardObj) {
			var ep = e.parent().parent();
			var itemData = _getCardData(ep, cardObj);
			e.bind('click', function(e) {
				if(_trigger(itemData, 'onEdit', cardObj) != false) {
				}
			});
		}
		
		//触发事件
		function _trigger(params, callback, cardObj) {
			if(!cardObj[callback]) {
				return;
			}
			return cardObj[callback].call(cardObj[callback], params) && true;
		}
		
		var card = {};
		
		//定义默认值
		var _defaults = {
			addUrl: '',	//如果添加列表的url为空，默认不添加任何内容
			addFormId: '',
			eidtUrl:'',
			editFormId: '',
			removeUrl: '',
			data: [],
			fields:{},
			
			//存放cards
			cards:[],
			onRemove: function(item) {},
			onEdit: function(item) {},
			onAdd: function() {}
		};
		
		//定义方法
		var _methods = {
			newInstance: newInstance,
			initCards: initCards
		};
		
		//初始化 card
		function _init() {
			for (var i in _defaults) {
				card[i] = _defaults[i];			
			}
			for (var i in _methods) {
				card[i] = _methods[i];			
			}
			return card;
		}
		
		return _init();
		
	});
})();